package com.yazi.mxz.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.myframework.smhj.common.tool.PageLink;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.StatusResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yazi.mxz.admin.jackson.filter.MemberFilter;
import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.enums.EmailStatus;
import com.yazi.mxz.common.entity.enums.Sex;
import com.yazi.mxz.common.service.MemberService;

@Controller("memberController")
@RequestMapping("/member")
public class MemberController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(MemberController.class);

	@Resource
	MemberService memberService;

	@RequestMapping("/list")
	public String list(Member member, Searching si, Model model) {
		log.debug("进入用户信息页面");
		// 默认按登录时间倒序
		if (null == si.getSorts()) {
			List<Sort> sorts = new ArrayList<Sort>();
			sorts.add(new Sort("loginTime", true));
			si.setSorts(sorts);
		}
		Page<Member> page = memberService.list(member, si);
		PageLink pageLink = new MyPageLink(page.getPaging());
		model.addAttribute("memberList", page.getList());
		model.addAttribute("pageLink", pageLink);
		model.addAttribute("searchWord", si.getSearchWord());
		return "member_list";
	}
	
	@RequestMapping("/list/json")
	@ResponseBody
	public ListResult<?> listJson(Member member, Searching si) {
		log.debug("获取用户列表");
		ListResult<Member> jsonResult = new ListResult<Member>();
		jsonResult.setFilter(MemberFilter.get());
		try {
			// 默认按登录时间倒序
			if (null == si.getSorts()) {
				List<Sort> sorts = new ArrayList<Sort>();
				sorts.add(new Sort("loginTime", true));
				si.setSorts(sorts);
			}
			Page<Member> page = memberService.list(member, si);
			jsonResult.setResult(page.getList());
		//	jsonResult.setPageInfo(page.getPaging);
			jsonResult.setOkMessage("ok");
		} catch (Exception e) {
			log.error("用户 -> 获取用户列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}
	
	@RequestMapping("/detail")
	public String detail(Long memberId, Model model) {
		log.debug("进入用户详情页面:memberId={}", memberId);
		Member member = memberService.one(memberId);
		EmailStatus[] emailStatuses = EmailStatus.values();
		Sex[] sexes = Sex.values();
		model.addAttribute("member", member);
		model.addAttribute("emailStatuses", emailStatuses);
		model.addAttribute("sexes", sexes);
		return "member_detail";
	}

	@RequestMapping("/editDo/json")
	@ResponseBody
	public StatusResult editDoJson(Member member) {
		log.debug("保存编辑后用户信息");
		StatusResult jsonResult = new StatusResult();
		try {
			memberService.merge(member);
			jsonResult.setOkMessage("修改成功");
		} catch (Exception e) {
			log.error("用户信息修改异常:", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}

}
